<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>排序算法之插入排序</title>
  </head>
  <body>
    <script type="text/javascript">
      function insertSort(arr) {
        for (let i = 1; i < arr.length; i++) {
          const value = arr[i];
          let j = 0; // 插入的位置
          for (j = i - 1; j >= 0 && arr[j] > value; j--) {
            arr[j + 1] = arr[j]; // 移动数据
          }
          arr[j + 1] = value; // 插入数据
        }
      }

      const arr = [10, 6, 3, 4, 8, 1, 2];
      insertSort(arr);
      console.log(arr);
    </script>
  </body>
</html>
